<template>
  <div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
      <el-tab-pane label="个人上传列表" name="first">
        <div class="container">
          <!--文件码对话框-->
          <el-dialog
            title="文件码"
            :visible.sync="dialogVisible"
            width="30%">
            <span>{{ this.fileNameInMinio }}</span>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">关闭</el-button>
  </span>
          </el-dialog>
          <!--任务列表-->
          <el-table
            :data="fileListData"
            height="250"
            border
            v-loading="loading"
            style="width: 100%">
            <el-table-column
              prop="id"
              label="编号"
              width="180">
            </el-table-column>
            <el-table-column
              prop="originFileName"
              label="文件名"
              width="280">
            </el-table-column>
            <el-table-column
              prop="uploadUserName"
              label="上传人"
              width="80"
            >
            </el-table-column>
            <el-table-column
              prop="fileSize"
              label="文件大小(单位 kb)"
              width="100"
            >
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="创建时间">
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              width="300">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="shareFile(scope.row)">分享</el-button>
                <el-button type="primary" size="small" @click="downloadFile(scope.row)">下载</el-button>
                <el-button type="danger" size="small" @click="handleRemoveFile(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[1, 10, 15]"
              :page-size="listQuery.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  import {
    downLoad,
    getFileForPackage,
    getFileList,
    getPersonalFileList,
    getUploadTaskList,
    removeFile
  } from "../../api/fileUpload";
  import {toDownLoad} from "../../utils/uploadUtil";

  export default {
    name: "",
    data() {
      return {
        loading:false,
        fileNameInMinio: '',
        activeName: 'first',
        dialogVisible: false,
        formLabelWidth: '150px',
        uploadTaskOptions: [],
        fileListData: [],
        total: 0,
        listQuery: {
          page: 1,
          size: 10,
          id: '',
          folder: ''
        }
      }
    },
    methods: {
      //切换标签
      handleTabClick(tab, event) {
        if(tab.index=='0'){
          this.getPersonalFileList()
        }else if (tab.index == '1') {
          this.getPersonalFileList()
        }
      },
      //分享文件
      shareFile(param) {
        console.log(param)
        this.fileNameInMinio = param.fileNameInMinio
        this.dialogVisible = true
      },
      //获取上传任务列表
      getTaskList() {
        getUploadTaskList({
          page: 1,
          size: 0
        })
          .then(res => {
            console.log(res)
            this.uploadTaskOptions = res.data.pageList;
          })
      },
      //任务文件上传列表
      getFileList() {
        this.getTaskList()
        this.loading=true
        getFileList(this.listQuery)
          .then(res => {
            if (res.code == 20000) {
              this.$message.success("查询成功");
              console.log(res)
              this.fileListData = res.data.pageList;
              this.total = res.data.total

            } else {
              this.$message.error(res.msg)
            }
            this.loading=false
          }).catch(err=>{
            this.loading=false
        })

      },
      //个人文件上传列表
      getPersonalFileList() {
        this.loading=true
        getPersonalFileList(this.listQuery)
          .then(res => {
            if (res.code == 20000) {
              this.$message.success("查询成功");
              console.log(res)
              this.fileListData = res.data.pageList;
              this.total = res.data.total

            } else {
              this.$message.error(res.msg)
            }
            this.loading=false
          }).catch(err=>{
            this.loading=false
        })
      },
      //下载文件
      downloadFile(param) {
        this.loading=true
        this.listQuery.id = param.id
        downLoad(this.listQuery).then(res => {
          console.log(res)
          this.binaryDownload(res)
          this.loading=false
        }).catch(err => {
          console.log(err)
          this.loading=false
        })
      },
      //下载函数
      binaryDownload(res) {
        if (!res.data) {
          this.$message.error("文件下载失败!")
          return
        }
        if (res.code != 20000) {
          this.$message.error(res.msg)
          return
        }
        toDownLoad(res);
      },
      //分页相关
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getFileList()
        console.log(`每页 ${val} 条`);
      },
      //打包获取文件
      getAllFile() {
        getFileForPackage(this.listQuery).then(res => {
          console.log(res)
          if (res.code != 20000) {
            this.$message.error(res.msg)
            return
          }
          toDownLoad(res)
        }).catch(err => {

        })

      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getFileList()
        console.log(`当前页: ${val}`);
      },
      //删除文件
      handleRemoveFile(param) {
        this.loading=true
        removeFile(param).then(res => {
          if (res.code == 20000) {
            this.$message.success("查询成功");
            console.log(res)
            this.getFileList()

          } else {
            this.$message.error(res.msg)
          }
          this.loading=false
        }).catch(err=>{
          this.loading=false
        })
      }
    },
    created() {
      this.getPersonalFileList()

    }
  }
</script>

<style scoped>
  .demo-form-inline {
    margin-top: 20px;
    margin-left: 10px;
  }
</style>
